﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization.Json;

namespace Backup_IOS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Data_Settings Settings
        {
            get
            {
                return App.Settings;
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            App.Settings = new Data_Settings()
            {
                Local_Backup_Path = System.IO.Path.Combine(Environment.CurrentDirectory, "Backup"),
                SSH_Host = "127.0.0.1",
                SSH_Password = "alpine",
                SSH_UserID = "root",
                Backup_AddressBook = true,
                Backup_Applications = new KeyValuePair<string,Guid>(),
                Backup_Calendar = true,
                Backup_Photos = true,
                Backup_SMS = true
            };
            Settings_Load();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Grid_Settings.DataContext = Settings;
        }

        private void OneClick_Backup(object sender, RoutedEventArgs e)
        {
            var w = new winOperation();
            w.PerformOperation("OneClick-Backup", null);
            w.Show();
        }

        private void OneClick_Restore(object sender, RoutedEventArgs e)
        {
            var w = new winOperation();
            w.PerformOperation("OneClick-Restore", null);
            w.Show();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Settings_Save();
        }

        void Settings_Save()
        {
            File.WriteAllText(System.IO.Path.Combine(Environment.CurrentDirectory, "settings.config"), Newtonsoft.Json.JsonConvert.SerializeObject(App.Settings));
        }

        void Settings_Load()
        {
            if (File.Exists(System.IO.Path.Combine(Environment.CurrentDirectory, "settings.config")))
            {
                try
                {
                    App.Settings = Newtonsoft.Json.JsonConvert.DeserializeObject<Data_Settings>(File.ReadAllText(System.IO.Path.Combine(Environment.CurrentDirectory, "settings.config")));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }
}
